﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.Shipping.BL;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for ShippingService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ShippingService : System.Web.Services.WebService
    {
        //string XSLT_DIR = "xslt";

        [WebMethod]
        public string GetShipmentHdrs(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            ShipmentManager shMgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                //dt = ShipmentHeaderDO.GetTableByColumns(sess, dcCond, null);
                shMgr = new ShipmentManager();
                dt = shMgr.GetShipmentHdrsByQFCri(sess, dcCond, null, ref stErr);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "SHIPMENT_HEADER";
                dt.DataSet.DataSetName = "SHIPMENT_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return "MSG_GEN000:" + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveShipmentHdr(string stSessID, string stRcpHdrXML)
        {
            ShipmentHeaderDO shpHdrDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();
            ShipmentManager shpMgr = null;

            try
            {
                shpHdrDO = (ShipmentHeaderDO)XMLDocManager.Deserialize(typeof(ShipmentHeaderDO), XMLDocManager._XML_HEADER + stRcpHdrXML);

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                shpMgr = new ShipmentManager();
                return shpMgr.SaveShipmentHdr(sess, shpHdrDO);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string SaveShipmentDtl(string stSessID, string stShpDtlXML)
        {
            ShipmentDetailDO shpDtlDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();

            try
            {
                shpDtlDO = (ShipmentDetailDO)XMLDocManager.Deserialize(typeof(ShipmentDetailDO), XMLDocManager._XML_HEADER + stShpDtlXML);

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (shpDtlDO.INTERNAL_SHIPMENT_LINE_NUM > 0)
                {
                    shpDtlDO.Update(sess);
                    return "" + shpDtlDO.INTERNAL_SHIPMENT_LINE_NUM;
                }
                else
                    return "" + shpDtlDO.Insert(sess);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetShipmentDtls(string stSessID, string stIntShpNum)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;
            int iIntRecNum = 0;

            try
            {
                if (StringManager.IsEmpty(stIntShpNum))
                    return "MSG_RCP_003";

                iIntRecNum = int.Parse(stIntShpNum);
                if (iIntRecNum < 1)
                    return "MSG_RCP_003";

                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = ShipmentDetailDO.GetTableByColumn(sess, "INTERNAL_SHIPMENT_NUM", stIntShpNum);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "SHIPMENT_DETAIL";
                dt.DataSet.DataSetName = "SHIPMENT_DETAILS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));

                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return "MSG_GEN000:" + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string GetShippingLoads(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = ShippingLoadDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "SHIPPING_LOAD";
                dt.DataSet.DataSetName = "SHIPPING_LOADS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveShippingLoad(string stSessID, string stShpLdXML)
        {
            ShippingLoadDO shpLdDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                shpLdDO = (ShippingLoadDO)XMLDocManager.Deserialize(typeof(ShippingLoadDO), XMLDocManager._XML_HEADER + stShpLdXML);
                
                if (shpLdDO.INTERNAL_LOAD_NUM > 0)
                {
                    shpLdDO.Update(sess);
                    return "" + shpLdDO.INTERNAL_LOAD_NUM;
                }
                else
                    return shpLdDO.Insert(sess);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string DeleteShipments(string stSessID, int[] arIntShpNum)
        {
            Session sess = null;
            ShipmentManager mgr = null;
            StringBuilder sbError = null;
            string stError = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new ShipmentManager();
                sbError = new StringBuilder();
                for (int i = 0; i < arIntShpNum.Length; i++)
                {
                    stError = mgr.DeleteShipment(sess, arIntShpNum[i]);
                    if (!StringManager.IsEmpty(stError))
                    {
                        sbError.AppendLine(stError);
                    }
                }

                if (StringManager.IsEmpty(sbError))
                    return null;
                else
                    return sbError.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string AddToWave(string stSessID, int[] arIntShpNum, int iWaveNum)
        {
            Session sess = null;
            ShipmentManager mgr = null;
            string stError = null;
            StringBuilder sbError = null;
            WaveHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                sbError = new StringBuilder();
                mgr = new ShipmentManager();
                stError = mgr.AddToWave(sess, arIntShpNum, iWaveNum);
                if (!StringManager.IsEmpty(stError))
                    sbError.AppendLine(stError);

                //更新波次统计信息
                helper = new WaveHelper();
                stError = helper.UpdateStatistics(sess, iWaveNum);
                if (!StringManager.IsEmpty(stError))
                {
                    sbError.AppendLine(stError);
                }

                if (sbError.Length < 1)
                    return null;
                else
                    return sbError.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string SaveWave(string stSessID, string stWarehouse, string stWaveFlow, string stWaveName)
        {
            Session sess = null;
            WaveDO wavDO = null;
            WaveFlowHeaderDO wfhDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                wfhDO = WaveFlowHeaderDO.GetModelByPK(sess, stWaveFlow);
                if (wfhDO == null)
                    return MessageManager.MSG_WAV_005; //波次流程无效

                wavDO = new WaveDO();
                wavDO.WAREHOUSE = stWarehouse;
                wavDO.STATUS = (int)Constants.WaveStatus.iWAVE_PENDING;
                wavDO.USER_STAMP = sess.User;
                wavDO.WAVE_FLOW = stWaveFlow;
                wavDO.WAVE_MODE = "Manual";
                wavDO.WAVE_NAME = stWaveName;
                wavDO.CREATE_TASK = wfhDO.CREATE_TASK;
                wavDO.DATE_TIME_STAMP = System.DateTime.Now;

                return wavDO.Insert(sess);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string RunWave(string stSessID, int iWaveNum, bool bAutoRelease)
        {
            Session sess = null;
            WaveHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new WaveHelper();
                return helper.RunWave(sess, iWaveNum, bAutoRelease);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string CancelWave(string stSessID, int iWaveNum)
        {
            Session sess = null;
            WaveHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new WaveHelper();
                return helper.CancelWave(sess, iWaveNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string DeleteWave(string stSessID, int iWaveNum)
        {
            Session sess = null;
            WaveHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new WaveHelper();
                return helper.DeleteWave(sess, iWaveNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string CancelShipmentFromWave(string stSessID, int[] arIntShpNum)
        {
            Session sess = null;
            ShippingHelper helper = null;
            StringBuilder sbErr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new ShippingHelper();
                sbErr = new StringBuilder();
                foreach (int iShpNum in arIntShpNum)
                {
                    stErr = helper.CancelShipmentFromWave(sess, iShpNum);
                    if (!StringManager.IsEmpty(stErr))
                        sbErr.AppendLine(stErr);
                }
                if (!StringManager.IsEmpty(sbErr))
                    return sbErr.ToString();
                else
                    return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string DeleteShipment(string stSessID, int iIntShpNum)
        {
            Session sess = null;
            ShipmentManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new ShipmentManager();
                return mgr.DeleteShipment(sess, iIntShpNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string DeleteShipmentDetail(string stSessID, int iIntShpLnNum)
        {
            Session sess = null;
            ShipmentManager mgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new ShipmentManager();
                return mgr.DeleteShipmentDtl(sess, iIntShpLnNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string ReleaseWave(string stSessID, int iWaveNum)
        {
            Session sess = null;
            WaveHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new WaveHelper();
                return helper.ReleaseWave(sess, iWaveNum);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetWaves(string stSessID, string stCondXML)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }

                dt = WaveDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;

                dt.TableName = "WAVE";
                dt.DataSet.DataSetName = "WAVES";
                sbXML = new StringBuilder();
                dt.WriteXml(new StringWriter(sbXML));

                return sbXML.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string AssignToLoad(string stSessID, string stIntLoadNum, int[] arIntShpNum)
        {
            Session sess = null;
            ShipmentManager shMgr = null;
            StringBuilder sbErr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                shMgr = new ShipmentManager();
                sbErr = new StringBuilder();
                for (int i = 0; i < arIntShpNum.Length; i++)
                {
                    stErr = shMgr.AssignToLoad(sess, Convert.ToInt32(stIntLoadNum), arIntShpNum[i]);
                    if (!StringManager.IsEmpty(stErr))
                        sbErr.AppendLine(stErr);
                }
                if (!StringManager.IsEmpty(sbErr))
                    return sbErr.ToString();
                else
                    return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string RemoveFromLoad(string stSessID, int stIntLoadNum, int[] arIntShpNum)
        {
            Session sess = null;
            ShipmentManager shMgr = null;
            StringBuilder sbErr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                shMgr = new ShipmentManager();
                sbErr = new StringBuilder();
                for (int i = 0; i < arIntShpNum.Length; i++)
                {
                    stErr = shMgr.RemoveFromLoad(sess, stIntLoadNum, arIntShpNum[i]);
                    if (!StringManager.IsEmpty(stErr))
                        sbErr.AppendLine(stErr);
                }
                if (!StringManager.IsEmpty(sbErr))
                    return sbErr.ToString();
                else
                    return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string CloseLoad(string stSessID, string stIntLoadNum)
        {
            Session sess = null;
            ShippingLoadManager slMgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                slMgr = new ShippingLoadManager();
                stErr = slMgr.CloseLoad(sess, Convert.ToInt32(stIntLoadNum));
                if (StringManager.IsEmpty(stErr))
                    return null;
                else
                    return stErr;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string DispatchLoad(string stSessID, string stIntLoadNum)
        {
            Session sess = null;
            ShippingLoadManager slMgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                slMgr = new ShippingLoadManager();
                return slMgr.ConfirmLoad(sess, Convert.ToInt32(stIntLoadNum));
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetWaveResult(string stSessID, string stIntWaveNum)
        {
            Session sess = null;
            WaveHelper helper = null;
            DataTable dt = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new WaveHelper();
                dt = helper.GetWaveResult(sess, Convert.ToInt32(stIntWaveNum));

                if (DataManager.IsEmpty(dt))
                    return MessageManager.MSG_GEN_001;
                else
                {
                    sbXML = new StringBuilder();
                    dt.TableName = "WAVE_RESULT";
                    dt.DataSet.DataSetName = "WAVE_RESULTS";
                    dt.DataSet.WriteXml(new StringWriter(sbXML));
                    return sbXML.ToString();
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string BuildWave(string stSessID, int[] arIntShpNum, string stWarehouse, string stWaveFlow)
        {
            Session sess = null;
            string stErr = null;
            int iWaveNum = 0;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (StringManager.IsEmpty(stWaveFlow))
                {
                    stWaveFlow = "*Default";
                }

                //创建波次
                stErr = this.SaveWave(stSessID, stWarehouse, stWaveFlow, "自动建立");
                if (StringManager.IsErrorMessage(stErr))
                    return stErr;

                //加入波次
                iWaveNum = Convert.ToInt32(stErr);
                stErr = this.AddToWave(stSessID, arIntShpNum, iWaveNum);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                //运行波次，自动释放
                stErr = this.RunWave(stSessID, iWaveNum, true);

                return stErr;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string CheckInventory(string stSessID, int iIntShpNum)
        {
            Session sess = null;
            ShippingHelper helper = null;
            DataTable dt = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (iIntShpNum <= 0)
                    return MessageManager.MSG_SHP_003;//错误：出库单不存在

                helper = new ShippingHelper();
                dt = helper.GetAvailableInventory(sess, iIntShpNum);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "INVENTORY";
                dt.DataSet.DataSetName = "INVENTORIES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetContainerDetails(string stSessID, string stCondXML)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            ShippingContainerManager scMgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }

                //dt = ShippingContainerDO.GetTableByColumns(sess, dcCond, null);
                scMgr = new ShippingContainerManager();
                dt = scMgr.GetShipContDtlByQFCri(sess, dcCond, null, ref stErr);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                if (DataManager.IsEmpty(dt))
                    return null;

                dt.TableName = "SHIPPING_CONTAINER";
                dt.DataSet.DataSetName = "SHIPPING_CONTAINERS";
                sbXML = new StringBuilder();
                dt.WriteXml(new StringWriter(sbXML));

                return sbXML.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetContainerHeaders(string stSessID, string stCondXML)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            ShippingContainerManager scMgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }

                //dt = ShippingContainerDO.GetTableByColumns(sess, dcCond, null);
                scMgr = new ShippingContainerManager();
                dt = scMgr.GetShipContHdrByQFCri(sess, dcCond, null, ref stErr);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                if (DataManager.IsEmpty(dt))
                    return null;

                dt.TableName = "SHIPPING_CONTAINER";
                dt.DataSet.DataSetName = "SHIPPING_CONTAINERS";
                sbXML = new StringBuilder();
                dt.WriteXml(new StringWriter(sbXML));

                return sbXML.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetPackingPendingDetail(string stSessID, string stShipmentID)
        {
            Session sess = null;
            DataTable dt = null;
            StringBuilder sbXML = null;
            //List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            PackingHelper mgr = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                mgr = new PackingHelper();
                dt = mgr.GetPackPendingDetails(sess, stShipmentID, ref stErr);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;

                sbXML = new StringBuilder();
                dt.DataSet.DataSetName = "PACKING_DETAILS";
                dt.TableName = "PACKING_DETAIL";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string PackItemToNewContainer(string stSessID, int iShpNum, string stContID, string stContType, string[] arPackingDS)
        {
            Session sess = null;
            PackingHelper helper = null;
            List<PackingDS> ltPackDS = null;
            PackingDS packDS = null;
            string stErr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                ltPackDS = new List<PackingDS>();
                foreach (string stPackDS in arPackingDS)
                {
                    packDS = (PackingDS)XMLDocManager.Deserialize(typeof(PackingDS), XMLDocManager._XML_HEADER + stPackDS);
                    ltPackDS.Add(packDS);
                }
                helper = new PackingHelper();
                return helper.PackItemToNewContainer(sess, iShpNum, stContID, stContType, ltPackDS);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string PackItemToContainer(string stSessID, int iContNum, string[] arPackingDS)
        {
            Session sess = null;
            List<PackingDS> ltPackDS = null;
            PackingDS packDS = null;
            PackingHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                ltPackDS = new List<PackingDS>();
                foreach (string stXML in arPackingDS)
                {
                    packDS = (PackingDS)XMLDocManager.Deserialize(typeof(PackingDS), XMLDocManager._XML_HEADER + stXML);
                    ltPackDS.Add(packDS);
                }

                helper = new PackingHelper();
                return helper.PackItemToContainer(sess, iContNum, null, ltPackDS);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string UnpackItem(string stSessID, int iContDtlNum, int iQty)
        {
            Session sess = null;
            PackingHelper helper = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                helper = new PackingHelper();
                return helper.UnpackItem(sess, iContDtlNum, iQty);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }
    }
}
